package com.example.app2024.widget;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.widget.RemoteViews;

import com.example.app2024.R;

import java.util.Date;

public class TestWidget extends AppWidgetProvider
{
      private static int aa = 1;
      public static final String WIDGET_BUTTON_ACTIONI = "widget_button_actioni";

      @Override
      public void onReceive(Context context, Intent intent)
      {
            super.onReceive(context, intent);
            if (intent != null && TextUtils.equals(intent.getAction(), WIDGET_BUTTON_ACTIONI))
            {
                  RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.layout_widget);


                  remoteViews.setTextViewText(R.id.widget_textview, "当前是:" +String.valueOf(new Date().toGMTString()));

                  AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
                  ComponentName componentName = new ComponentName(context, TestWidget.class);
                  appWidgetManager.updateAppWidget(componentName, remoteViews);

            }
      }

      @Override
      public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
      {
            super.onUpdate(context, appWidgetManager, appWidgetIds);
            RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.layout_widget);
            Intent intent = new Intent();
            intent.setClass(context, TestWidget.class); //发给本身
            intent.setAction(WIDGET_BUTTON_ACTIONI);
            PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent,  PendingIntent.FLAG_IMMUTABLE);
            remoteViews.setOnClickPendingIntent(R.id.widget_button, pendingIntent);
            appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
      }
}
